package com.cjj.zoompic.widgets;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.ScrollView;

import com.cjj.zoompic.R;
import com.cjj.zoompic.utils.LogEx;
import com.nineoldandroids.animation.ObjectAnimator;
import com.nineoldandroids.animation.ValueAnimator;

/**
 * 下拉缩放图片的ScrollView
 *
 * @Author：Created by cjj on 16/1/12.
 * @Blog：jianjunchen.net
 */
public class ZoomPicScrollView extends ScrollView {
    private static final String tag = "ZoomPicScrollView";
    private static final float PIC_OFFSET = 0.6f;//图片iv_pic滑动距离的一个偏移系数

    private ImageView iv_pic;//要缩放的图片
    private float initPosition;//初始图片距离顶部位置

    private int width_pic = 0;//图片原宽度
    private int heigth_pic = 0;//图片原高度
    private boolean init = true;//是否是初始化
    // 是否正在缩放
    private Boolean mScaling = false;

    public ZoomPicScrollView(Context context) {
        this(context, null, 0);
    }

    public ZoomPicScrollView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ZoomPicScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(final Context context) {
        this.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if (init) {
                    iv_pic = (ImageView) ZoomPicScrollView.this.findViewById(R.id.iv_pic);

                    // 获取屏幕宽高
                    DisplayMetrics metric = new DisplayMetrics();
                    ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(metric);
                    //设置图片初始大小  这里我设为满屏的16:9
                    ViewGroup.LayoutParams lp = iv_pic.getLayoutParams();
                    width_pic = lp.width = metric.widthPixels;
                    heigth_pic = lp.height = metric.widthPixels * 9 / 16;
                    iv_pic.setLayoutParams(lp);
                    init = false;
                    LogEx.d(tag, "图片原宽度：" + width_pic + ";图片原高度：" + heigth_pic);
                }
            }
        });

    }

    /**
     * 动画复原
     */
    private void restore() {
        final ViewGroup.LayoutParams lp_pic = iv_pic.getLayoutParams();
        final float w = iv_pic.getLayoutParams().width;// 图片当前宽度
        final float h = iv_pic.getLayoutParams().height;// 图片当前高度
        LogEx.d(tag,"图片当前宽度："+w+";图片当前高度："+h);

        // 设置动画
        ValueAnimator anim = ObjectAnimator.ofFloat(0.0f, 1.0f).setDuration(200);

        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float cVal = (Float) animation.getAnimatedValue();
                lp_pic.width = (int) (w - (w - width_pic) * cVal);
                lp_pic.height = (int) (h - (h - heigth_pic) * cVal);
                iv_pic.setLayoutParams(lp_pic);
            }
        });
        anim.start();
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_MOVE://
                if (!mScaling) {//没在缩放（正常的滑动）
                    int scrollY = this.getScrollY();
                    LogEx.d(tag, "scrollY:" + scrollY);
                    if (scrollY == 0) {//在初始状态时(未滑动/停止滑动)
                        initPosition = ev.getY();
                        LogEx.d(tag, "初始图片距离顶部位置：" + initPosition);
                    }else {
                        break;
                    }
                }
                //缩放图片和VG
                int distance_pic = (int) ((ev.getY() - initPosition) * PIC_OFFSET);
                LogEx.d(tag,"图片要滑动的距离："+distance_pic);
                if (distance_pic < 0) {//正常滑动，不进行图片缩放
                    break;
                }
                mScaling = true;
                ViewGroup.LayoutParams lp_pic = iv_pic.getLayoutParams();
                lp_pic.width = width_pic + distance_pic;
                lp_pic.height = (width_pic + distance_pic) * 9 / 16;
                iv_pic.setLayoutParams(lp_pic);
                return true;
            case MotionEvent.ACTION_UP://
                mScaling = false;
                restore();
                break;
        }
        return super.onTouchEvent(ev);
    }
}
